package GreedyThought;

import java.util.Arrays;
import java.util.Comparator;

// Lettcode 435: https://leetcode-cn.com/problems/non-overlapping-intervals/
public class eraseOverlapIntervals {
    public int eraseOverlapIntervals(int[][] intervals) {
        if (intervals.length == 0) return 0;
        Arrays.sort(intervals, new Comparator<int[]>() {
            @Override
            public int compare(int[] ints, int[] t1) {
                return ints[1] - t1[1];
            }
        });
        int res = 1;
        int end = intervals[0][1];
        for (int i = 0; i < intervals.length; i++) {
            if (intervals[i][0] < end) {
                continue;
            }
            end = intervals[i][1];
            res++;
        }
        return intervals.length - res;
    }
}
